package com.zc.study.designPattern.行为型设计模式.MementoPattern;

public class Test {
    private static int index = -1; //定义一个索引来记录当前状态所在的位置
    private static  MementoCaretaker mc = new MementoCaretaker();
    
    
    public static void main(String[] args) {
        MementoCaretaker mc = new MementoCaretaker();
        Chessman chess = new Chessman("车", 1, 1);
        play(chess);
        mc.setMemento(chess.save());//保存状态
        chess.setY(4);
        play(chess);
        chess.setX(5);
        play(chess);
        uodo(chess,index);
        uodo(chess,index);
    }


    //下棋
    private static void play(Chessman chess) {
        mc.setMemento(chess.save()); //保存备忘录
        index++;
        System.out.println("棋子" + chess.getLabel() + "当前的位置为:第" + chess.getX() +
                "行,第" + chess.getY() + "列."
        );
    }
    
    //悔棋

    private static void uodo(Chessman chess, int i) {
        System.out.println("*****悔棋******");
        index--;
        chess.restore(mc.getMemento(i - 1));
        System.out.println("棋子" + chess.getLabel() + "当前的位置为:第" + chess.getX() +
                "行,第" + chess.getY() + "列."
        );
    }
        
}
